<?php

namespace app\system\controller;

use app\common\tool\Tool;
use app\system\model\AdminModel;
use think\Controller;

class Admin extends Controller
{
    use Tool;
    //管理员列表
    public function index(){
        $data = input('');
        //默认
        $pageNow = 1;//当前页
        $pageSize = 10;//一页多少行
        $lineCount = 0;//总记录数
        $pageCount = 0;//总页数
        if(!empty($data)){
            if(isset($data['pageNow'])){
                $pageNow = $data['pageNow'];
            }
            if(isset($data['pageSize'])){
                $pageSize = $data['pageSize'];
            }
        }

        $lineCount = AdminModel::count();
        $pageCount = ceil($lineCount / $pageSize);
        $data = AdminModel::with('roles')->order('id', 'desc')->limit(($pageNow-1) * $pageSize,$pageSize)->select();
        return $this->returnJson(200, '管理员列表', [
            'pageNow' => (int)$pageNow,
            'pageSize' => (int)$pageSize,
            'lineCount' => (int)$lineCount,
            'pageCount' => (int)$pageCount,
            'data' => $data,
        ]);
    }

    public function add(){
        $username = input('username');
        $password = input('password');
        $status = input('status');
        $role = input('role');

        if(!isset($username, $password)){
            return $this->returnJson(500, '用户名, 密码不能为空');
        }
        if(AdminModel::get(['username' => $username])){
            return $this->returnJson(500, '登录用户名已经存在');
        }
        if(!isset($status)){
            $status = 1;
        }
        $admin = AdminModel::create([
            'username' => $username,
            'password' => md5($password),
            'status' => $status
        ]);
        if(!$admin){
            return $this->returnJson(500, '插入数据库失败');
        }
        //角色关联
        $admin->roles()->detach();
        if(!empty($role)){
            $admin->roles()->saveAll($role);
        }
        return $this->returnJson(200, '管理员创建成功',$admin);
    }

    public function edit(){
        $id = input('id');
        $password = input('password');
        $status = input('status');
        $role = input('role');

        $admin = AdminModel::get($id);
        if(!$admin){
            return $this->returnJson(500,'该管理员不存在');
        }
        if(empty($status)){
            $status = 1;
        }
        if(!empty($password)){
            $admin->password = md5($password);
        }
        $admin->status = $status;
        $admin->save();
        //角色关联
        $admin->roles()->detach();
        if(!empty($role)){
            $admin->roles()->saveAll($role);
        }
        return $this->returnJson(200, '管理员修改成功', $admin);
    }

    public function del(){
        $id = input('id');
        $admin = AdminModel::get($id);
        if(!$admin){
            return $this->returnJson(500,'该管理员不存在');
        }
        $admin->delete();
        return $this->returnJson(200,'管理员删除成功');
    }

    public function changeStatus(){
        $id = input('id');
        $admin = AdminModel::get($id);
        if(!$admin){
            return $this->returnJson(500,'该管理员不存在');
        }
        $admin->status = $admin->status == 1 ? 0 : 1;
        $admin->save();
        return $this->returnJson(200,'管理员状态修改成功',$admin);
    }
}
